package com.basic.library.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.regex.Pattern;

public class StringUtils {

	/**
	 * 把输入流转换成字符数组
	 * @param inputStream  输入流
	 * @return 字符数组
	 * @throws Exception
	 */
	public static byte[] readStream(InputStream inputStream) throws Exception {
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inputStream.read(buffer)) != -1) {
			bout.write(buffer, 0, len);
		}
		bout.close();
		inputStream.close();

		return bout.toByteArray();
	}

	public static String replaceUrlWithPlus(String url) {
		if (url != null) {
			// return url.replaceAll("http://(.)*?/",
			// "").replaceAll("[.:/,%?&=]", "+").replaceAll("[+]+", "+");
			return url.replaceAll("[/|&|?|:|%]+", "_");
		}
		return null;
	}

	// 获得汉语拼音首字母
	public static String getAlpha(String str) {
		if (str == null) {
			return "#";
		}

		if (str.trim().length() == 0) {
			return "#";
		}

		char c = str.trim().substring(0, 1).charAt(0);
		// 正则表达式，判断首字母是否是英文字母
		Pattern pattern = Pattern.compile("^[A-Za-z]+$");
		if (pattern.matcher(c + "").matches()) {
			return (c + "").toUpperCase();
		} else {
			return "#";
		}
	}

	/**
	 * 判断给定字符串是否空白串。
	 * 空白串是指由空格、制表符、回车符、换行符组成的字符串
	 * 若输入字符串为null或空字符串，返回true
	 * @param input
	 * @return boolean
	 */
	public static boolean isEmpty( String input ) 
	{
		if ( input == null || "".equals( input ) )
			return true;
		
		for ( int i = 0; i < input.length(); i++ ) 
		{
			char c = input.charAt( i );
			if ( c != ' ' && c != '\t' && c != '\r' && c != '\n' )
			{
				return false;
			}
		}
		return true;
	}
	
	/*
	 * 规格化时间
	 */
	public static String  calTime(long endTime){
	    long between=endTime-System.currentTimeMillis();
	     int tmp=(int) (between/(1000*60*60*24));
	     between=between%(1000*60*60*24);
	     String timeStr="";
	     if(tmp>0)
	     {
		     timeStr+=String.valueOf(tmp)+"天";
	     }
	     
	     tmp=(int) (between/(1000*60*60));
	     between=between%(1000*60*60);
	     if(tmp>0)
	     {
		     timeStr+= String.valueOf(tmp)+"小时";
	     }
	     
	     
	     tmp=(int) (between/(1000*60));
	     between=between%(1000*60);
	     if(tmp>0)
	     {
		     timeStr+=  String.valueOf(tmp)+"分";
	     }
	     
	     tmp=(int) (between/(1000));
	     if(tmp>0)
	     {
		     timeStr+=  String.valueOf(tmp)+"秒";
	     }
	     return timeStr;
    }
	
	public static String getCurrentTime() {
		  SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		  dateformat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		  Date date = new Date(System.currentTimeMillis());
		  return "最近更新："+dateformat.format(date);
	}

	/**
	 * 左补位
	 * @param str
	 * @param length
	 * @param c
	 * @return
	 */
	public static String padLeft(String str, int length, char c) {
		int count = length - str.length();
		for (int i = 0; i < count; i++) {
			str = c + str;
		}
		return str;
	}
}
